/*
 * =============================================================================
 * Copyright© by Benjamin Sempere,
 * All rights reserved.
 * =============================================================================
 * Author  : Benjamin Sempere
 * E-mail  : benjamin@sempere.org
 * Homepage: www.sempere.org
 * =============================================================================
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.sempere.gwt.toolbox.core.client.common;

import java.util.HashMap;
import java.util.Map;

import org.sempere.gwt.toolbox.utils.client.StringUtils;

import com.google.gwt.core.client.GWT;

public abstract class AbstractLocator<T extends Locatee> implements Locator<T> {

	protected Map<String, T> locateesMap = new HashMap<String, T>();

	/**
	 * Default class constructor
	 */
	public AbstractLocator() {
	}

	public void register(T locatee) {
		if (locatee == null) {
			throw new IllegalArgumentException("Cannot add locatee when it is null.");
		}
		if (this.locateesMap.containsKey(locatee.getLocateeName())) {
			throw new IllegalArgumentException("The locatee with name [" + locatee.getLocateeName() + "] is already registered to the locator with name [" + this.getLocatorName() + "].");
		}

		this.locateesMap.put(locatee.getLocateeName(), locatee);
		GWT.log("The locatee with name [" + locatee.getLocateeName() + "] was successfully registered to the locator with name [" + this.getLocatorName() + "].", null);
	}

	public void unregister(T locatee) {
		if (locatee == null) {
			throw new IllegalArgumentException("Cannot remove locatee when it is null.");
		}
		if (!this.locateesMap.containsKey(locatee.getLocateeName())) {
			throw new IllegalArgumentException("No locatee with name [" + locatee.getLocateeName() + "] is registered to the locator with name [" + this.getLocatorName() + "].");
		}

		this.locateesMap.remove(locatee.getLocateeName());
		GWT.log("The locatee with name [" + locatee.getLocateeName() + "] was successfully unregistered to the locator with name [" + this.getLocatorName() + "].", null);
	}

	public void clear() {
		this.locateesMap.clear();
		GWT.log("The locator with name [" + this.getLocatorName() + "] was successfully cleared.", null);
	}

	public T locate(String locateeName) {
		if (StringUtils.isBlank(locateeName)) {
			throw new IllegalArgumentException("Cannot get locatee when its name is blank.");
		}
		if (!this.locateesMap.containsKey(locateeName)) {
			throw new IllegalArgumentException("No locatee with name [" + locateeName + "] is registered to the locator with name [" + this.getLocatorName() + "].");
		}

		T locatee = this.locateesMap.get(locateeName);
		GWT.log("The locatee with name [" + locateeName + "] was successfully retrieved from to the locator with name [" + this.getLocatorName() + "].", null);

		return locatee;
	}
}